﻿using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelephoneYiShou;

namespace customer
{
    public partial class cusClear : Skin_Mac
    {
        public cusClear()
        {
            InitializeComponent();
        }

        DataTable dtss = null;
        int i, start;//i表示总行数，start表示当前位置
        int size = 10;//size表示一页显示的数量        
        string str1 = "select * from Client";
        string str = "server=.;database = TelephoneYiShou;uid = sa;pwd = 123456";

        private void btn_ck_Click(object sender, EventArgs e)
        {
            string type = txt_check.Text;
            //查询
            string strkey = txt_check.Text;//找到检索的关键字
            if (string.IsNullOrEmpty(strkey))//判断关键字是否为空
            {
                MessageBoxEx.Show("请输入关键字");
                return;//直接返回到窗口
            }
            //按姓名方式
            if (cmb_type.Text == "姓名")
            {
                str1 = string.Format("select * from Client where ClientName = '{0}';", type);
                dtss = DBHelper.GetDataTable(str1);
                i = dtss.Rows.Count;//获取数据的条数
                if (i == 0)
                {
                    MessageBoxEx.Show("没有" + type + "的相关信息");
                }
                else
                {
                    show(0, size);
                    labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
                }
            }
            //按性别查找
            if (cmb_type.Text == "性别")
            {
                str1 = string.Format("select * from Client where Gender = '{0}';", type);
                dtss = DBHelper.GetDataTable(str1);
                i = dtss.Rows.Count;//获取数据的条数
                if (i == 0)
                {
                    MessageBoxEx.Show("没有" + type + "的相关信息");
                }
                else
                {
                    show(0, size);
                    labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
                }
            }
            //按地址查找
            if (cmb_type.Text == "地址")
            {
                str1 = string.Format("select * from Client where Adderss = '{0}';", type);
                dtss = DBHelper.GetDataTable(str1);
                i = dtss.Rows.Count;//获取数据的条数
                if (i == 0)
                {
                    MessageBoxEx.Show("没有" + type + "地址信息");
                }
                else
                {
                    show(0, size);
                    labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
                }
            }

            
            if (dtss.Rows.Count == 0)
            {
                dgv_sheet.DataSource = dtss;//清空数据
                //不能用占位符
                MessageBoxEx.Show("没有查找到" + strkey + "相关信息");
                
                return;
            }
            dgv_sheet.DataSource = dtss;//如果有数据，则刷新数据绑定控件
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            //退出
            this.Close();
        }

        private void cusClear_Load(object sender, EventArgs e)
        {
            string strsql = "select * from Client";
            DataTable dt = DBHelper.GetDataTable(strsql);
            dgv_sheet.DataSource = dt;
        }

        //首页
        private void button1_Click(object sender, EventArgs e)
        {
            show(0, size);//调用show函数
            start = 0;
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }

        //上一页
        private void btn_up_Click(object sender, EventArgs e)
        {
            if (start < 1)
            {
                MessageBoxEx.Show("已是第一页.");
            }
            else
            {
                start = start - 1;
                show(start * size, size);
            }
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }

        //下一页
        private void btn_down_Click(object sender, EventArgs e)
        {
            if (start + 1 > i / size)
            {
                MessageBoxEx.Show("已是最后页");
            }
            else
            {
                start = start + 1;
                show(start * size, size);
            }
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }

        //尾页
        private void btn_end_Click(object sender, EventArgs e)
        {
            show(i / size, i - (i / size) * size);
            start = i / size;
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }

        //显示i到j的数据
        public void show(int i, int j)
        {
            SqlConnection sqlcon = new SqlConnection(str);
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand(str1, sqlcon);
            SqlDataAdapter sqlada = new SqlDataAdapter(sqlcmd);
            DataSet ds = new DataSet();
            sqlada.Fill(ds, i, j, "[SellGoods]");//把数据库表中的i-j的记录数用来填充数据集
            dgv_sheet.DataSource = ds.Tables["[SellGoods]"];
            ds = null;//清空数据集
        }
    }
}
